<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class loggamescores extends Admin_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	var $model = "mloggamescores";
	var $view = "loggamescores";
	var $resource = "";
    function __construct() {
        parent::__construct();
        $this->load->model($this->model);
        $this->load->model("mcategory");
		$this->resCode = $this->{$this->model}->resCode;
        $this->url_index = admin_url() . $this->view;
        $this->folder_upload = "dataupload/".$this->view."/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
		$this->url_search = $this->url_index . "/search";
		$this->resource = $this->mresource->get($this->resCode,'RES_CODE');
    }

    public function index($page = 0) {
		$this->check_permission();
        $data = array();
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = 20;
        $config['cur_page'] = $page;
        $config['base_url'] = $this->url_index.'/index/';
		
		$this->search();
        $config['total_rows'] = $this->{$this->model}->num();
        $this->pagination->initialize($config);
		
		$this->search();
		$this->db->join("game as g","g.game_id = log_game_scores.game_id");
		$this->db->join("user as u","u.use_id = log_game_scores.use_id");
		$this->db->order_by('log_gscores_id desc');
        $data['datas'] = $this->{$this->model}->pagination('','',$config);
        $this->theme_data["main"] = $this->view."/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($editID = 0) {
		$this->check_permission($editID);
        $data = array();
		redirect($this->url_index);
        $post = $this->input->post();
        if ($post) {
			if($post['edit']['game_code'] ==''){
					$post['edit']['game_code'] = $this->sseo->seolink($post['edit']['game_title']);
			}
            if ($editID > 0) {
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				
				$editID = $this->{$this->model}->insert($post['edit']);
			}
            redirect($this->url_edit . $editID);
        }
		$this->db->join("game as g","g.game_id = loggamescores.game_id");
		$this->db->join("user as u","u.use_id = loggamescores.use_id");
		$data['edit'] = $this->{$this->model}->get($editID,'loggamescores.game_id');
		  
        $this->theme_data["main"] = $this->view."/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($deleteID = 0){
		$this->check_permission();
		if($deleteID > 0){
			$this->{$this->model}->delete($deleteID);
		}
	}
	
	function upload_file($old_name = '', $newName = '') {
		$config = array();
		$config['upload_path'] = './'.$this->folder_upload."";
		$config['allowed_types'] = 'swf';
		$config['overwrite'] = true;
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
			
		if($old_name && isset($old_name)){
			unlink('./'.$old_name);
		}
		
		$this->load->library('upload');
		$this->upload->initialize($config);
		if ( !$this->upload->do_upload())
		{
			return false;
		}
		else
		{
			
			return array('upload_data' => $this->upload->data());
		}
	}
	
	function upload_my_file($game_id = 0){
	
		$type = "";
		require(APPPATH.'libraries/FileUpload.php');
		$game = $this->mgame->get($game_id,'game_id');
		if(isset($_FILES['game_thumb']) || isset($_GET['game_thumb']))
		{
			$uploader = new FileUpload('game_thumb');
			$type = "game_thumb";
			$uploader->newFileName = $game->game_code.'-'.$game->game_id.'.'.$uploader->getExtension();
			$this->folder_upload = $this->folder_upload."thumb/";
		}
		else if(isset($_FILES['game_file'])|| isset($_GET['game_file']))
		{
			$uploader = new FileUpload('game_file');
			$this->folder_upload = $this->folder_upload."data/";
			$type = "game_file";
		}
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		if($type =="")
			die("null");
		$uploader->allowedExtensions = array('jpg', 'jpeg', 'png', 'gif','swf');
		$uploader->sizeLimit = 21071520;
		//$uploader->sizeLimit = 1;
		//$uploader->newFileName = 'newFile.jpg';
		$result = $uploader->handleUpload('./'.$this->folder_upload);
		if (!$result) {
		echo json_encode(array(
		  'success' => false,
		  'msg' => $uploader->getErrorMsg()
		));    

		} else {
		$arr_insert = array();
		$arr_insert[$type] = $this->folder_upload.''.$uploader->getFileName();
		$this->mgame->update($game_id,'game_id',$arr_insert);
		echo json_encode(array(
			'success' => true,
			'file' => base_url().$this->folder_upload.''.$uploader->getFileName()
		 ));
		}
	}
	
	function upload_process(){
	
	if (isset($_SERVER['HTTP_ORIGIN'])) {
  header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
  header('Access-Control-Allow-Credentials: true');
  header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

if (isset($_REQUEST['progresskey'])) 
  $status = apc_fetch('upload_'.$_REQUEST['progresskey']);
else 
  exit(json_encode(array('success' => false)));

$pct = 0;
$size = 0;

if (is_array($status)) {
  if (array_key_exists('total', $status) && array_key_exists('current', $status)) {
    if ($status['total'] > 0) {
      $pct = round( ( $status['current'] / $status['total']) * 100 );
      $size = round($status['total'] / 1024);
    }
  }
}

echo json_encode(array('success' => true, 'pct' => $pct, 'size' => $size));

}
}
